package forestsimulatorp3.model.simulator;

import model.enums.Season;
import model.enums.Weather;



/**
 * Controls the wather
 * @author dielson
 *
 */
public class WeatherController {

	// Attributes --------------------------------------------------------------

	private static Weather currentWeather = generateNewWeather();
	private static int timeUntilNextWeather = generateTimeUntilNextWeather();

	// Public methods ----------------------------------------------------------

	public static Weather getCurrentWeather() {
		return currentWeather;
	}

	public static int getTimeUntilNextWeather() {
		return timeUntilNextWeather;
	}

	public static void advanceCycle() {
		if (getTimeUntilNextWeather() == 0) {
			currentWeather = generateNewWeather();
			timeUntilNextWeather = generateTimeUntilNextWeather();
		} else {
			timeUntilNextWeather--;
		}
	}

	/**
	 * Force the controller to set a new {@link Weather}
	 * @param currentWeather
	 */
	public static void setCurrentWeather(Weather currentWeather) {
		WeatherController.currentWeather = currentWeather;
	}

	/**
	 * Set the time until the next weather change.
	 * @param timeUntilNextWeather the number of cycles that the current 
	 * {@link Weather} will last
	 */
	public static void setTimeUntilNextWeather(int timeUntilNextWeather) {
		WeatherController.timeUntilNextWeather = timeUntilNextWeather;
	}

	// Private method ----------------------------------------------------------

	/**
	 * Generates a new {@link Weather} based on the currentSeason
	 */
	private static Weather generateNewWeather() {

		int random = (int) (Math.random() * 10);
		
		if (Time.getCurrentSeason() == Season.SPRING ||
				Time.getCurrentSeason() == Season.AUTUMN) {

			if (random <= 3) {
				return Weather.RAINY;
			} else if (random > 3 && random <= 6) {
				return Weather.CLOUDY;
			} else {
				return Weather.SUNNY;
			}

		} else if (Time.getCurrentSeason() == Season.SUMMER) {

			if (random <= 2) {
				return Weather.RAINY;
			} else if (random > 2 && random <= 6) {
				return Weather.CLOUDY;
			} else {
				return Weather.SUNNY;
			}

		} else { // Season.WINTER

			if (random <= 3) {
				return Weather.SUNNY;
			} else if (random > 3 && random <= 6) {
				return Weather.CLOUDY;
			} else {
				return Weather.SNOWY;
			}

		}
	}

	private static int generateTimeUntilNextWeather() {
		return (int) ((Math.random() * 10)/2 + 1);
	}
}
